home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / emacs / 19.22 / lisp / mouse.el < prev    next >
Lisp/Scheme  |  1993-11-26  |  54KB  |  1,389 lines

  1. ;;; mouse.el --- window system-independent mouse support.
  2.  
  3. ;;; Copyright (C) 1993 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: hardware
  7.  
  8. ;;; This file is part of GNU Emacs.
  9.  
  10. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  11. ;;; it under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 2, or (at your option)
  13. ;;; any later version.
  14.  
  15. ;;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ;;; GNU General Public License for more details.
  19.  
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Emacs; see the file COPYING.  If not, write to
  22. ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  23.  
  24. ;;; Commentary:
  25.  
  26. ;; This package provides various useful commands (including help
  27. ;; system access) through the mouse.  All this code assumes that mouse
  28. ;; interpretation has been abstracted into Emacs input events.
  29. ;;
  30. ;; The code is rather X-dependent.
  31.  
  32. ;;; Code:
  33.  
  34. ;;; Utility functions.
  35.  
  36. ;;; Indent track-mouse like progn.
  37. (put 'track-mouse 'lisp-indent-function 0)
  38.  
  39.  
  40. (defun mouse-delete-window (click)
  41.   "Delete the window you click on.
  42. This must be bound to a mouse click."
  43.   (interactive "e")
  44.   (delete-window (posn-window (event-start click))))
  45.  
  46. (defun mouse-tear-off-window (click)
  47.   "Delete the window clicked on, and create a new frame displaying its buffer."
  48.   (interactive "e")
  49.   (let* ((window (posn-window (event-start click)))
  50.      (buf (window-buffer window))
  51.      (frame (new-frame)))
  52.     (select-frame frame)
  53.     (switch-to-buffer buf)
  54.     (delete-window window)))
  55.  
  56. (defun mouse-delete-other-windows ()
  57.   "Delete all window except the one you click on."
  58.   (interactive "@")
  59.   (delete-other-windows))
  60.  
  61. (defun mouse-split-window-vertically (click)
  62.   "Select Emacs window mouse is on, then split it vertically in half.
  63. The window is split at the line clicked on.
  64. This command must be bound to a mouse click."
  65.   (interactive "@e")
  66.   (let ((start (event-start click)))
  67.     (select-window (posn-window start))
  68.     (let ((new-height (if (eq (posn-point start) 'vertical-scroll-bar)
  69.               (scroll-bar-scale (posn-col-row start)
  70.                         (1- (window-height)))
  71.             (1+ (cdr (posn-col-row (event-end click))))))
  72.       (first-line window-min-height)
  73.       (last-line (- (window-height) window-min-height)))
  74.       (if (< last-line first-line)
  75.       (error "window too short to split")
  76.     (split-window-vertically
  77.      (min (max new-height first-line) last-line))))))
  78.  
  79. (defun mouse-split-window-horizontally (click)
  80.   "Select Emacs window mouse is on, then split it horizontally in half.
  81. The window is split at the column clicked on.
  82. This command must be bound to a mouse click."
  83.   (interactive "@e")
  84.   (let ((start (event-start click)))
  85.     (select-window (posn-window start))
  86.     (let ((new-width (1+ (car (posn-col-row (event-end click)))))
  87.       (first-col window-min-width)
  88.       (last-col (- (window-width) window-min-width)))
  89.       (if (< last-col first-col)
  90.       (error "window too narrow to split")
  91.     (split-window-horizontally
  92.      (min (max new-width first-col) last-col))))))
  93.  
  94. (defun mouse-set-point (event)
  95.   "Move point to the position clicked on with the mouse.
  96. This should be bound to a mouse click event type."
  97.   (interactive "e")
  98.   ;; Use event-end in case called from mouse-drag-region.
  99.   ;; If EVENT is a click, event-end and event-start give same value.
  100.   (let ((posn (event-end event)))
  101.     (and (window-minibuffer-p (posn-window posn))
  102.      (not (minibuffer-window-active-p (posn-window posn)))
  103.      (error "Minibuffer window is not active"))
  104.     (select-window (posn-window posn))
  105.     (if (numberp (posn-point posn))
  106.     (goto-char (posn-point posn)))))
  107.  
  108. (defun mouse-set-region (click)
  109.   "Set the region to the text dragged over, and copy to kill ring.
  110. This should be bound to a mouse drag event."
  111.   (interactive "e")
  112.   (let ((posn (event-start click))
  113.     (end (event-end click)))
  114.     (select-window (posn-window posn))
  115.     (if (numberp (posn-point posn))
  116.     (goto-char (posn-point posn)))
  117.     ;; If mark is highlighted, no need to bounce the cursor.
  118.     (or (and transient-mark-mode
  119.          (eq (framep (selected-frame)) 'x))
  120.     (sit-for 1))
  121.     (push-mark)
  122.     (set-mark (point))
  123.     (if (numberp (posn-point end))
  124.     (goto-char (posn-point end)))
  125.     ;; Don't set this-command to kill-region, so that a following
  126.     ;; C-w will not double the text in the kill ring.
  127.     (let (this-command)
  128.       (copy-region-as-kill (mark) (point)))))
  129.  
  130. (defvar mouse-scroll-delay 0.25
  131.   "*The pause between scroll steps caused by mouse drags, in seconds.
  132. If you drag the mouse beyond the edge of a window, Emacs scrolls the
  133. window to bring the text beyond that edge into view, with a delay of
  134. this many seconds between scroll steps.  Scrolling stops when you move
  135. the mouse back into the window, or release the button.
  136. This variable's value may be non-integral.
  137. Setting this to zero causes Emacs to scroll as fast as it can.")
  138.  
  139. (defun mouse-scroll-subr (jump &optional overlay start)
  140.   "Scroll the selected window JUMP lines at a time, until new input arrives.
  141. If OVERLAY is an overlay, let it stretch from START to the far edge of
  142. the newly visible text.
  143. Upon exit, point is at the far edge of the newly visible text."
  144.   (while (progn
  145.        (goto-char (window-start))
  146.        (if (not (zerop (vertical-motion jump)))
  147.            (progn
  148.          (set-window-start (selected-window) (point))
  149.          (if (natnump jump)
  150.              (progn
  151.                (goto-char (window-end (selected-window)))
  152.                ;; window-end doesn't reflect the window's new
  153.                ;; start position until the next redisplay.  Hurrah.
  154.                (vertical-motion (1- jump)))
  155.            (goto-char (window-start (selected-window))))
  156.          (if overlay
  157.              (move-overlay overlay start (point)))
  158.          (if (not (eobp))
  159.              (sit-for mouse-scroll-delay))))))
  160.   (point))
  161.  
  162. (defvar mouse-drag-overlay (make-overlay 1 1))
  163. (overlay-put mouse-drag-overlay 'face 'region)
  164.  
  165. (defvar mouse-selection-click-count 0)
  166.  
  167. (defun mouse-drag-region (start-event)
  168.   "Set the region to the text that the mouse is dragged over.
  169. Highlight the drag area as you move the mouse.
  170. This must be bound to a button-down mouse event.
  171. In Transient Mark mode, the highlighting remains once you
  172. release the mouse button.  Otherwise, it does not."
  173.   (interactive "e")
  174.   (let* ((start-posn (event-start start-event))
  175.      (start-point (posn-point start-posn))
  176.      (start-window (posn-window start-posn))
  177.      (start-frame (window-frame start-window))
  178.      (bounds (window-edges start-window))
  179.      (top (nth 1 bounds))
  180.      (bottom (if (window-minibuffer-p start-window)
  181.              (nth 3 bounds)
  182.            ;; Don't count the mode line.
  183.            (1- (nth 3 bounds))))
  184.      (click-count (1- (event-click-count start-event))))
  185.     (setq mouse-selection-click-count click-count)
  186.     (mouse-set-point start-event)
  187.     (let ((range (mouse-start-end start-point start-point click-count)))
  188.       (move-overlay mouse-drag-overlay (car range) (nth 1 range)
  189.             (window-buffer start-window)))
  190.     (deactivate-mark)
  191.     (let (event end end-point)
  192.       (track-mouse
  193.     (while (progn
  194.          (setq event (read-event))
  195.          (or (mouse-movement-p event)
  196.              (eq (car-safe event) 'switch-frame)))
  197.       (if (eq (car-safe event) 'switch-frame)
  198.           nil
  199.         (setq end (event-end event)
  200.           end-point (posn-point end))
  201.  
  202.         (cond
  203.  
  204.          ;; Ignore switch-frame events.
  205.          ((eq (car-safe event) 'switch-frame))
  206.  
  207.          ;; Are we moving within the original window?
  208.          ((and (eq (posn-window end) start-window)
  209.            (integer-or-marker-p end-point))
  210.           (goto-char end-point)
  211.           (let ((range (mouse-start-end start-point (point) click-count)))
  212.         (move-overlay mouse-drag-overlay (car range) (nth 1 range))))
  213.  
  214.          ;; Are we moving on a different window on the same frame?
  215.          ((and (windowp (posn-window end))
  216.            (eq (window-frame (posn-window end)) start-frame))
  217.           (let ((mouse-row
  218.              (+ (nth 1 (window-edges (posn-window end)))
  219.             (cdr (posn-col-row end)))))
  220.         (cond
  221.          ((< mouse-row top)
  222.           (mouse-scroll-subr
  223.            (- mouse-row top) mouse-drag-overlay start-point))
  224.          ((and (not (eobp))
  225.                (>= mouse-row bottom))
  226.           (mouse-scroll-subr (1+ (- mouse-row bottom))
  227.                      mouse-drag-overlay start-point)))))
  228.  
  229.          (t
  230.           (let ((mouse-y (cdr (cdr (mouse-position))))
  231.             (menu-bar-lines (or (cdr (assq 'menu-bar-lines
  232.                            (frame-parameters)))
  233.                     0)))
  234.  
  235.         ;; Are we on the menu bar?
  236.         (and (integerp mouse-y) (< mouse-y menu-bar-lines)
  237.              (mouse-scroll-subr (- mouse-y menu-bar-lines)
  238.                     mouse-drag-overlay start-point))))))))
  239.  
  240.       (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
  241.            (eq (posn-window (event-end event)) start-window)
  242.            (numberp (posn-point (event-end event))))
  243.       (let ((fun (key-binding (vector (car event)))))
  244.         (if (memq fun '(mouse-set-region mouse-set-point))
  245.         (if (not (= (overlay-start mouse-drag-overlay)
  246.                 (overlay-end mouse-drag-overlay)))
  247.             (let (this-command)
  248.               (push-mark (overlay-start mouse-drag-overlay) t t)
  249.               (goto-char (overlay-end mouse-drag-overlay))
  250.               (copy-region-as-kill (point) (mark t)))
  251.           (goto-char (overlay-end mouse-drag-overlay))
  252.           (setq this-command 'mouse-set-point))
  253.           (if (fboundp fun)
  254.           (funcall fun event)))))
  255.       (delete-overlay mouse-drag-overlay))))
  256.  
  257. ;; Commands to handle xterm-style multiple clicks.
  258.  
  259. (defun mouse-skip-word (dir)
  260.   "Skip over word, over whitespace, or over identical punctuation.
  261. If DIR is positive skip forward; if negative, skip backward."
  262.   (let* ((char (following-char))
  263.      (syntax (char-to-string (char-syntax char))))
  264.     (if (or (string= syntax "w") (string= syntax " "))
  265.     (if (< dir 0)
  266.         (skip-syntax-backward syntax)
  267.       (skip-syntax-forward syntax))
  268.       (if (< dir 0)
  269.       (while (and (not (bobp)) (= (preceding-char) char))
  270.         (forward-char -1))
  271.     (while (and (not (eobp)) (= (following-char) char))
  272.       (forward-char 1))))))
  273.  
  274. ;; Return a list of region bounds based on START and END according to MODE.
  275. ;; If MODE is 0 then set point to (min START END), mark to (max START END).
  276. ;; If MODE is 1 then set point to start of word at (min START END),
  277. ;; mark to end of word at (max START END).
  278. ;; If MODE is 2 then do the same for lines.
  279. (defun mouse-start-end (start end mode)
  280.   (if (> start end)
  281.       (let ((temp start))
  282.         (setq start end
  283.               end temp)))
  284.   (setq mode (mod mode 3))
  285.   (cond ((= mode 0)
  286.      (list start end))
  287.         ((and (= mode 1)
  288.               (= start end)
  289.           (not (eobp))
  290.               (= (char-syntax (char-after start)) ?\())
  291.      (list start (save-excursion (forward-sexp 1) (point))))
  292.         ((and (= mode 1)
  293.               (= start end)
  294.           (not (eobp))
  295.               (= (char-syntax (char-after start)) ?\)))
  296.      (list (save-excursion 
  297.          (goto-char (1+ start))
  298.          (backward-sexp 1)
  299.          (point))
  300.            (1+ start)))
  301.         ((= mode 1)
  302.      (list (save-excursion
  303.          (goto-char start)
  304.          (mouse-skip-word -1)
  305.          (point))
  306.            (save-excursion
  307.          (goto-char end)
  308.          (mouse-skip-word 1)
  309.          (point))))
  310.         ((= mode 2)
  311.      (list (save-excursion
  312.          (goto-char start)
  313.          (beginning-of-line 1)
  314.          (point))
  315.            (save-excursion
  316.          (goto-char end)
  317.          (forward-line 1)
  318.          (point))))))
  319.  
  320. ;; Subroutine: set the mark where CLICK happened,
  321. ;; but don't do anything else.
  322. (defun mouse-set-mark-fast (click)
  323.   (let ((posn (event-start click)))
  324.     (select-window (posn-window posn))
  325.     (if (numberp (posn-point posn))
  326.     (push-mark (posn-point posn) t t))))
  327.  
  328. ;; Momentarily show where the mark is, if highlighting doesn't show it. 
  329. (defun mouse-show-mark ()
  330.   (or transient-mark-mode
  331.       (save-excursion
  332.     (goto-char (mark t))
  333.     (sit-for 1))))
  334.  
  335. (defun mouse-set-mark (click)
  336.   "Set mark at the position clicked on with the mouse.
  337. Display cursor at that position for a second.
  338. This must be bound to a mouse click."
  339.   (interactive "e")
  340.   (let ((point-save (point)))
  341.     (unwind-protect
  342.     (progn (mouse-set-point click)
  343.            (push-mark nil t t)
  344.            (or transient-mark-mode
  345.            (sit-for 1)))
  346.       (goto-char point-save))))
  347.  
  348. (defun mouse-kill (click)
  349.   "Kill the region between point and the mouse click.
  350. The text is saved in the kill ring, as with \\[kill-region]."
  351.   (interactive "e")
  352.   (let ((click-posn (posn-point (event-start click))))
  353.     (if (numberp click-posn)
  354.     (kill-region (min (point) click-posn)
  355.              (max (point) click-posn)))))
  356.  
  357. (defun mouse-yank-at-click (click arg)
  358.   "Insert the last stretch of killed text at the position clicked on.
  359. Prefix arguments are interpreted as with \\[yank]."
  360.   (interactive "e\nP")
  361.   (mouse-set-point click)
  362.   (setq this-command 'yank)
  363.   (yank arg))
  364.  
  365. (defun mouse-kill-ring-save (click)
  366.   "Copy the region between point and the mouse click in the kill ring.
  367. This does not delete the region; it acts like \\[kill-ring-save]."
  368.   (interactive "e")
  369.   (mouse-set-mark-fast click)
  370.   (kill-ring-save (point) (mark t))
  371.   (mouse-show-mark))
  372.  
  373. ;;; This function used to delete the text between point and the mouse
  374. ;;; whenever it was equal to the front of the kill ring, but some
  375. ;;; people found that confusing.
  376.  
  377. ;;; A list (TEXT START END), describing the text and position of the last
  378. ;;; invocation of mouse-save-then-kill.
  379. (defvar mouse-save-then-kill-posn nil)
  380.  
  381. (defun mouse-save-then-kill-delete-region (beg end)
  382.   ;; We must make our own undo boundaries
  383.   ;; because they happen automatically only for the current buffer.
  384.   (undo-boundary)
  385.   (if (or (= beg end) (eq buffer-undo-list t))
  386.       ;; If we have no undo list in this buffer,
  387.       ;; just delete.
  388.       (delete-region beg end)
  389.     ;; Delete, but make the undo-list entry share with the kill ring.
  390.     ;; First, delete just one char, so in case buffer is being modified
  391.     ;; for the first time, the undo list records that fact.
  392.     (delete-region beg
  393.            (+ beg (if (> end beg) 1 -1)))
  394.     (let ((buffer-undo-list buffer-undo-list))
  395.       ;; Undo that deletion--but don't change the undo list!
  396.       (primitive-undo 1 buffer-undo-list)
  397.       ;; Now delete the rest of the specified region,
  398.       ;; but don't record it.
  399.       (setq buffer-undo-list t)
  400.       (if (/= (length (car kill-ring)) (- (max end beg) (min end beg)))
  401.       (error "Lossage in mouse-save-then-kill-delete-region"))
  402.       (delete-region beg end))
  403.     (let ((tail buffer-undo-list))
  404.       ;; Search back in buffer-undo-list for the string
  405.       ;; that came from deleting one character.
  406.       (while (and tail (not (stringp (car (car tail)))))
  407.     (setq tail (cdr tail)))
  408.       ;; Replace it with an entry for the entire deleted text.
  409.       (and tail
  410.        (setcar tail (cons (car kill-ring) (min beg end))))))
  411.   (undo-boundary))
  412.  
  413. (defun mouse-save-then-kill (click)
  414.   "Save text to point in kill ring; the second time, kill the text.
  415. If the text between point and the mouse is the same as what's
  416. at the front of the kill ring, this deletes the text.
  417. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  418. which prepares for a second click to delete the text.
  419.  
  420. If you have selected words or lines, this command extends the
  421. selection through the word or line clicked on.  If you do this
  422. again in a different position, it extends the selection again.
  423. If you do this twice in the same position, the selection is killed." 
  424.   (interactive "e")
  425.   (let ((click-posn (posn-point (event-start click)))
  426.     ;; Don't let a subsequent kill command append to this one:
  427.     ;; prevent setting this-command to kill-region.
  428.     (this-command this-command))
  429.     (if (> (mod mouse-selection-click-count 3) 0)
  430.     (if (not (and (eq last-command 'mouse-save-then-kill)
  431.               (equal click-posn
  432.                  (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  433.         ;; Find both ends of the object selected by this click.
  434.         (let* ((range
  435.             (mouse-start-end click-posn click-posn
  436.                      mouse-selection-click-count)))
  437.           ;; Move whichever end is closer to the click.
  438.           ;; That's what xterm does, and it seems reasonable.
  439.           (if (< (abs (- click-posn (mark t)))
  440.              (abs (- click-posn (point))))
  441.           (set-mark (car range))
  442.         (goto-char (nth 1 range)))
  443.           ;; We have already put the old region in the kill ring.
  444.           ;; Replace it with the extended region.
  445.           ;; (It would be annoying to make a separate entry.)
  446.           (setcar kill-ring (buffer-substring (point) (mark t)))
  447.           (if interprogram-cut-function
  448.           (funcall interprogram-cut-function (car kill-ring)))
  449.           ;; Arrange for a repeated mouse-3 to kill this region.
  450.           (setq mouse-save-then-kill-posn
  451.             (list (car kill-ring) (point) click-posn))
  452.           (mouse-show-mark))
  453.       ;; If we click this button again without moving it,
  454.       ;; that time kill.
  455.       (mouse-save-then-kill-delete-region (point) (mark))
  456.       (setq mouse-selection-click-count 0)
  457.       (setq mouse-save-then-kill-posn nil))
  458.       (if (and (eq last-command 'mouse-save-then-kill)
  459.            mouse-save-then-kill-posn
  460.            (eq (car mouse-save-then-kill-posn) (car kill-ring))
  461.            (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  462.       ;; If this is the second time we've called
  463.       ;; mouse-save-then-kill, delete the text from the buffer.
  464.       (progn
  465.         (mouse-save-then-kill-delete-region (point) (mark))
  466.         ;; After we kill, another click counts as "the first time".
  467.         (setq mouse-save-then-kill-posn nil))
  468.     (if (or (and (eq last-command 'mouse-save-then-kill)
  469.              mouse-save-then-kill-posn)
  470.         (and mark-active transient-mark-mode)
  471.         (and (eq last-command 'mouse-drag-region)
  472.              (or mark-even-if-inactive
  473.              (not transient-mark-mode))))
  474.         ;; We have a selection or suitable region, so adjust it.
  475.         (let* ((posn (event-start click))
  476.            (new (posn-point posn)))
  477.           (select-window (posn-window posn))
  478.           (if (numberp new)
  479.           (progn
  480.             ;; Move whichever end of the region is closer to the click.
  481.             ;; That is what xterm does, and it seems reasonable.
  482.             (if (< (abs (- new (point))) (abs (- new (mark t))))
  483.             (goto-char new)
  484.               (set-mark new))
  485.             (setq deactivate-mark nil)))
  486.           (setcar kill-ring (buffer-substring (point) (mark t)))
  487.           (if interprogram-cut-function
  488.           (funcall interprogram-cut-function (car kill-ring))))
  489.       ;; We just have point, so set mark here.
  490.       (mouse-set-mark-fast click)
  491.       (kill-ring-save (point) (mark t))
  492.       (mouse-show-mark))
  493.     (setq mouse-save-then-kill-posn
  494.           (list (car kill-ring) (point) click-posn))))))
  495.  
  496. (global-set-key [M-mouse-1] 'mouse-start-secondary)
  497. (global-set-key [M-drag-mouse-1] 'mouse-set-secondary)
  498. (global-set-key [M-down-mouse-1] 'mouse-drag-secondary)
  499. (global-set-key [M-mouse-3] 'mouse-secondary-save-then-kill)
  500. (global-set-key [M-mouse-2] 'mouse-yank-secondary)
  501.  
  502. ;; An overlay which records the current secondary selection
  503. ;; or else is deleted when there is no secondary selection.
  504. ;; May be nil.
  505. (defvar mouse-secondary-overlay nil)
  506.  
  507. ;; A marker which records the specified first end for a secondary selection.
  508. ;; May be nil.
  509. (defvar mouse-secondary-start nil)
  510.  
  511. (defun mouse-start-secondary (click)
  512.   "Set one end of the secondary selection to the position clicked on.
  513. Use \\[mouse-secondary-save-then-kill] to set the other end
  514. and complete the secondary selection."
  515.   (interactive "e")
  516.   (let ((posn (event-start click)))
  517.     (save-excursion
  518.       (set-buffer (window-buffer (posn-window posn)))
  519.       ;; Cancel any preexisting secondary selection.
  520.       (if mouse-secondary-overlay
  521.       (delete-overlay mouse-secondary-overlay))
  522.       (if (numberp (posn-point posn))
  523.       (progn
  524.         (or mouse-secondary-start
  525.         (setq mouse-secondary-start (make-marker)))
  526.         (move-marker mouse-secondary-start (posn-point posn)))))))
  527.  
  528. (defun mouse-set-secondary (click)
  529.   "Set the secondary selection to the text that the mouse is dragged over.
  530. This must be bound to a mouse drag event."
  531.   (interactive "e")
  532.   (let ((posn (event-start click))
  533.     beg
  534.     (end (event-end click)))
  535.     (save-excursion
  536.       (set-buffer (window-buffer (posn-window posn)))
  537.       (if (numberp (posn-point posn))
  538.       (setq beg (posn-point posn)))
  539.       (if mouse-secondary-overlay
  540.       (move-overlay mouse-secondary-overlay beg (posn-point end))
  541.     (setq mouse-secondary-overlay (make-overlay beg (posn-point end))))
  542.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  543.  
  544. (defun mouse-drag-secondary (start-event)
  545.   "Set the secondary selection to the text that the mouse is dragged over.
  546. Highlight the drag area as you move the mouse.
  547. This must be bound to a button-down mouse event."
  548.   (interactive "e")
  549.   (let* ((start-posn (event-start start-event))
  550.      (start-point (posn-point start-posn))
  551.      (start-window (posn-window start-posn))
  552.      (start-frame (window-frame start-window))
  553.      (bounds (window-edges start-window))
  554.      (top (nth 1 bounds))
  555.      (bottom (if (window-minibuffer-p start-window)
  556.              (nth 3 bounds)
  557.            ;; Don't count the mode line.
  558.            (1- (nth 3 bounds))))
  559.      (click-count (1- (event-click-count start-event))))
  560.     (save-excursion
  561.       (set-buffer (window-buffer start-window))
  562.       (setq mouse-selection-click-count click-count)
  563.       (or mouse-secondary-overlay
  564.       (setq mouse-secondary-overlay
  565.         (make-overlay (point) (point))))
  566.       (overlay-put mouse-secondary-overlay 'face 'secondary-selection)
  567.       (if (> (mod click-count 3) 0)
  568.       ;; Double or triple press: make an initial selection
  569.       ;; of one word or line.
  570.       (let ((range (mouse-start-end start-point start-point click-count)))
  571.         (set-marker mouse-secondary-start nil)
  572.         (move-overlay mouse-secondary-overlay 1 1
  573.               (window-buffer start-window))
  574.         (move-overlay mouse-secondary-overlay (car range) (nth 1 range)
  575.               (window-buffer start-window)))
  576.     ;; Single-press: cancel any preexisting secondary selection.
  577.     (or mouse-secondary-start
  578.         (setq mouse-secondary-start (make-marker)))
  579.     (set-marker mouse-secondary-start start-point)
  580.     (delete-overlay mouse-secondary-overlay))
  581.       (let (event end end-point)
  582.     (track-mouse
  583.       (while (progn
  584.            (setq event (read-event))
  585.            (or (mouse-movement-p event)
  586.                (eq (car-safe event) 'switch-frame)))
  587.  
  588.         (if (eq (car-safe event) 'switch-frame)
  589.         nil
  590.           (setq end (event-end event)
  591.             end-point (posn-point end))
  592.           (cond
  593.  
  594.            ;; Ignore switch-frame events.
  595.            ((eq (car-safe event) 'switch-frame))
  596.  
  597.            ;; Are we moving within the original window?
  598.            ((and (eq (posn-window end) start-window)
  599.              (integer-or-marker-p end-point))
  600.         (if (/= start-point end-point)
  601.             (set-marker mouse-secondary-start nil))
  602.         (let ((range (mouse-start-end start-point end-point
  603.                           click-count)))
  604.           (move-overlay mouse-secondary-overlay
  605.                 (car range) (nth 1 range))))
  606.  
  607.            ;; Are we moving on a different window on the same frame?
  608.            ((and (windowp (posn-window end))
  609.              (eq (window-frame (posn-window end)) start-frame))
  610.         (let ((mouse-row
  611.                (+ (nth 1 (window-edges (posn-window end)))
  612.               (cdr (posn-col-row end)))))
  613.           (cond
  614.            ((< mouse-row top)
  615.             (mouse-scroll-subr
  616.              (- mouse-row top) mouse-secondary-overlay start-point))
  617.            ((and (not (eobp))
  618.              (>= mouse-row bottom))
  619.             (mouse-scroll-subr (1+ (- mouse-row bottom))
  620.                        mouse-drag-overlay start-point)))))
  621.  
  622.            (t
  623.         (let ((mouse-y (cdr (cdr (mouse-position))))
  624.               (menu-bar-lines (or (cdr (assq 'menu-bar-lines
  625.                              (frame-parameters)))
  626.                       0)))
  627.  
  628.           ;; Are we on the menu bar?
  629.           (and (integerp mouse-y) (< mouse-y menu-bar-lines)
  630.                (mouse-scroll-subr (- mouse-y menu-bar-lines)
  631.                       mouse-secondary-overlay start-point))))))))
  632.  
  633.     (if (and (eq (get (event-basic-type event) 'event-kind) 'mouse-click)
  634.          (eq (posn-window (event-end event)) start-window)
  635.          (numberp (posn-point (event-end event))))
  636.         (if (marker-position mouse-secondary-start)
  637.         (save-window-excursion
  638.           (delete-overlay mouse-secondary-overlay)
  639.           (x-set-selection 'SECONDARY nil)
  640.           (select-window start-window)
  641.           (save-excursion
  642.             (goto-char mouse-secondary-start)
  643.             (sit-for 1)))
  644.           (x-set-selection
  645.            'SECONDARY
  646.            (buffer-substring (overlay-start mouse-secondary-overlay)
  647.                  (overlay-end mouse-secondary-overlay)))))))))
  648.  
  649. (defun mouse-yank-secondary (click)
  650.   "Insert the last stretch of killed text at the position clicked on.
  651. Prefix arguments are interpreted as with \\[yank]."
  652.   (interactive "e")
  653.   (save-excursion
  654.     (set-buffer (window-buffer (posn-window (event-start click))))
  655.     (goto-char (posn-point (event-start click)))
  656.     (insert (x-get-selection 'SECONDARY))))
  657.  
  658. (defun mouse-kill-secondary (click)
  659.   "Kill the text in the secondary selection.
  660. This is intended more as a keyboard command than as a mouse command
  661. but it can work as either one.
  662.  
  663. The current buffer (in case of keyboard use), or the buffer clicked on,
  664. must be the one that the secondary selection is in.  This requirement
  665. is to prevent accidents."
  666.   (interactive "e")
  667.   (or (eq (overlay-buffer mouse-secondary-overlay)
  668.       (if (listp click)
  669.           (window-buffer (posn-window (event-start click)))
  670.         (current-buffer)))
  671.       (error "Select or click on the buffer where the secondary selection is"))
  672.   (save-excursion
  673.     (set-buffer (overlay-buffer mouse-secondary-overlay))
  674.     (kill-region (overlay-start mouse-secondary-overlay)
  675.          (overlay-end mouse-secondary-overlay)))
  676.   (delete-overlay mouse-secondary-overlay)
  677.   (x-set-selection 'SECONDARY nil)
  678.   (setq mouse-secondary-overlay nil))
  679.  
  680. (defun mouse-secondary-save-then-kill (click)
  681.   "Save text to point in kill ring; the second time, kill the text.
  682. If the text between point and the mouse is the same as what's
  683. at the front of the kill ring, this deletes the text.
  684. Otherwise, it adds the text to the kill ring, like \\[kill-ring-save],
  685. which prepares for a second click to delete the text.
  686.  
  687. If you have selected words or lines, this command extends the
  688. selection through the word or line clicked on.  If you do this
  689. again in a different position, it extends the selection again.
  690. If you do this twice in the same position, the selection is killed." 
  691.   (interactive "e")
  692.   (let ((posn (event-start click))
  693.     (click-posn (posn-point (event-start click)))
  694.     ;; Don't let a subsequent kill command append to this one:
  695.     ;; prevent setting this-command to kill-region.
  696.     (this-command this-command))
  697.     (or (eq (window-buffer (posn-window posn))
  698.         (or (and mouse-secondary-overlay
  699.              (overlay-buffer mouse-secondary-overlay))
  700.         (if mouse-secondary-start
  701.             (marker-buffer mouse-secondary-start))))
  702.     (error "Wrong buffer"))
  703.     (save-excursion
  704.       (set-buffer (window-buffer (posn-window posn)))
  705.       (if (> (mod mouse-selection-click-count 3) 0)
  706.       (if (not (and (eq last-command 'mouse-secondary-save-then-kill)
  707.             (equal click-posn
  708.                    (car (cdr-safe (cdr-safe mouse-save-then-kill-posn))))))
  709.           ;; Find both ends of the object selected by this click.
  710.           (let* ((range
  711.               (mouse-start-end click-posn click-posn
  712.                        mouse-selection-click-count)))
  713.         ;; Move whichever end is closer to the click.
  714.         ;; That's what xterm does, and it seems reasonable.
  715.         (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  716.                (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  717.             (move-overlay mouse-secondary-overlay (car range)
  718.                   (overlay-end mouse-secondary-overlay))
  719.           (move-overlay mouse-secondary-overlay
  720.                 (overlay-start mouse-secondary-overlay)
  721.                 (nth 1 range)))
  722.         ;; We have already put the old region in the kill ring.
  723.         ;; Replace it with the extended region.
  724.         ;; (It would be annoying to make a separate entry.)
  725.         (setcar kill-ring (buffer-substring
  726.                    (overlay-start mouse-secondary-overlay)
  727.                    (overlay-end mouse-secondary-overlay)))
  728.         (if interprogram-cut-function
  729.             (funcall interprogram-cut-function (car kill-ring)))
  730.         ;; Arrange for a repeated mouse-3 to kill this region.
  731.         (setq mouse-save-then-kill-posn
  732.               (list (car kill-ring) (point) click-posn)))
  733.         ;; If we click this button again without moving it,
  734.         ;; that time kill.
  735.         (progn
  736.           (mouse-save-then-kill-delete-region
  737.            (overlay-start mouse-secondary-overlay)
  738.            (overlay-end mouse-secondary-overlay))
  739.           (setq mouse-save-then-kill-posn nil)
  740.           (setq mouse-selection-click-count 0)
  741.           (delete-overlay mouse-secondary-overlay)))
  742.     (if (and (eq last-command 'mouse-secondary-save-then-kill)
  743.          mouse-save-then-kill-posn
  744.          (eq (car mouse-save-then-kill-posn) (car kill-ring))
  745.          (equal (cdr mouse-save-then-kill-posn) (list (point) click-posn)))
  746.         ;; If this is the second time we've called
  747.         ;; mouse-secondary-save-then-kill, delete the text from the buffer.
  748.         (progn
  749.           (mouse-save-then-kill-delete-region
  750.            (overlay-start mouse-secondary-overlay)
  751.            (overlay-end mouse-secondary-overlay))
  752.           (setq mouse-save-then-kill-posn nil)
  753.           (delete-overlay mouse-secondary-overlay))
  754.       (if (overlay-start mouse-secondary-overlay)
  755.           ;; We have a selection, so adjust it.
  756.           (progn
  757.         (if (numberp click-posn)
  758.             (progn
  759.               ;; Move whichever end of the region is closer to the click.
  760.               ;; That is what xterm does, and it seems reasonable.
  761.               (if (< (abs (- click-posn (overlay-start mouse-secondary-overlay)))
  762.                  (abs (- click-posn (overlay-end mouse-secondary-overlay))))
  763.               (move-overlay mouse-secondary-overlay click-posn
  764.                     (overlay-end mouse-secondary-overlay))
  765.             (move-overlay mouse-secondary-overlay
  766.                       (overlay-start mouse-secondary-overlay)
  767.                       click-posn))
  768.               (setq deactivate-mark nil)))
  769.         (setcar kill-ring (buffer-substring
  770.                    (overlay-start mouse-secondary-overlay)
  771.                    (overlay-end mouse-secondary-overlay)))
  772.         (if interprogram-cut-function
  773.             (funcall interprogram-cut-function (car kill-ring))))
  774.         (if mouse-secondary-start
  775.         ;; All we have is one end of a selection,
  776.         ;; so put the other end here.
  777.         (let ((start (+ 0 mouse-secondary-start)))
  778.           (kill-ring-save start click-posn)
  779.           (if mouse-secondary-overlay
  780.               (move-overlay mouse-secondary-overlay start click-posn)
  781.             (setq mouse-secondary-overlay (make-overlay start click-posn)))
  782.           (overlay-put mouse-secondary-overlay 'face 'secondary-selection))))
  783.       (setq mouse-save-then-kill-posn
  784.         (list (car kill-ring) (point) click-posn))))
  785.       (x-set-selection 'SECONDARY
  786.                (if (overlay-buffer mouse-secondary-overlay)
  787.                (buffer-substring
  788.                 (overlay-start mouse-secondary-overlay)
  789.                 (overlay-end mouse-secondary-overlay)))))))
  790.  
  791. (defun mouse-buffer-menu (event)
  792.   "Pop up a menu of buffers for selection with the mouse.
  793. This switches buffers in the window that you clicked on,
  794. and selects that window."
  795.   (interactive "e")
  796.   (let ((menu
  797.      (list "Buffer Menu"
  798.            (cons "Select Buffer"
  799.              (let ((tail (buffer-list))
  800.                (maxbuf 0)
  801.                head)
  802.                (while tail
  803.              (or (eq ?\ (aref (buffer-name (car tail)) 0))
  804.                  (setq maxbuf
  805.                    (max maxbuf
  806.                     (length (buffer-name (car tail))))))
  807.              (setq tail (cdr tail)))
  808.                (setq tail (buffer-list))
  809.                (while tail
  810.              (let ((elt (car tail)))
  811.                (if (not (string-match "^ "
  812.                           (buffer-name elt)))
  813.                    (setq head (cons
  814.                        (cons
  815.                         (format
  816.                          (format "%%%ds  %%s%%s  %%s"
  817.                              maxbuf)
  818.                          (buffer-name elt)
  819.                          (if (buffer-modified-p elt)
  820.                          "*" " ")
  821.                          (save-excursion
  822.                            (set-buffer elt)
  823.                            (if buffer-read-only "%" " "))
  824.                          (or (buffer-file-name elt) ""))
  825.                         elt)
  826.                        head))))
  827.              (setq tail (cdr tail)))
  828.                (reverse head))))))
  829.     (let ((buf (x-popup-menu event menu))
  830.       (window (posn-window (event-start event))))
  831.       (if buf
  832.       (progn
  833.         (or (framep window) (select-window window))
  834.         (switch-to-buffer buf))))))
  835.  
  836. ;;; These need to be rewritten for the new scroll bar implementation.
  837.  
  838. ;;;!! ;; Commands for the scroll bar.
  839. ;;;!! 
  840. ;;;!! (defun mouse-scroll-down (click)
  841. ;;;!!   (interactive "@e")
  842. ;;;!!   (scroll-down (1+ (cdr (mouse-coords click)))))
  843. ;;;!! 
  844. ;;;!! (defun mouse-scroll-up (click)
  845. ;;;!!   (interactive "@e")
  846. ;;;!!   (scroll-up (1+ (cdr (mouse-coords click)))))
  847. ;;;!! 
  848. ;;;!! (defun mouse-scroll-down-full ()
  849. ;;;!!   (interactive "@")
  850. ;;;!!   (scroll-down nil))
  851. ;;;!! 
  852. ;;;!! (defun mouse-scroll-up-full ()
  853. ;;;!!   (interactive "@")
  854. ;;;!!   (scroll-up nil))
  855. ;;;!! 
  856. ;;;!! (defun mouse-scroll-move-cursor (click)
  857. ;;;!!   (interactive "@e")
  858. ;;;!!   (move-to-window-line (1+ (cdr (mouse-coords click)))))
  859. ;;;!! 
  860. ;;;!! (defun mouse-scroll-absolute (event)
  861. ;;;!!   (interactive "@e")
  862. ;;;!!   (let* ((pos (car event))
  863. ;;;!!      (position (car pos))
  864. ;;;!!      (length (car (cdr pos))))
  865. ;;;!!     (if (<= length 0) (setq length 1))
  866. ;;;!!     (let* ((scale-factor (max 1 (/ length (/ 8000000 (buffer-size)))))
  867. ;;;!!        (newpos (* (/ (* (/ (buffer-size) scale-factor)
  868. ;;;!!                 position)
  869. ;;;!!              length)
  870. ;;;!!               scale-factor)))
  871. ;;;!!       (goto-char newpos)
  872. ;;;!!       (recenter '(4)))))
  873. ;;;!! 
  874. ;;;!! (defun mouse-scroll-left (click)
  875. ;;;!!   (interactive "@e")
  876. ;;;!!   (scroll-left (1+ (car (mouse-coords click)))))
  877. ;;;!! 
  878. ;;;!! (defun mouse-scroll-right (click)
  879. ;;;!!   (interactive "@e")
  880. ;;;!!   (scroll-right (1+ (car (mouse-coords click)))))
  881. ;;;!! 
  882. ;;;!! (defun mouse-scroll-left-full ()
  883. ;;;!!   (interactive "@")
  884. ;;;!!   (scroll-left nil))
  885. ;;;!! 
  886. ;;;!! (defun mouse-scroll-right-full ()
  887. ;;;!!   (interactive "@")
  888. ;;;!!   (scroll-right nil))
  889. ;;;!! 
  890. ;;;!! (defun mouse-scroll-move-cursor-horizontally (click)
  891. ;;;!!   (interactive "@e")
  892. ;;;!!   (move-to-column (1+ (car (mouse-coords click)))))
  893. ;;;!! 
  894. ;;;!! (defun mouse-scroll-absolute-horizontally (event)
  895. ;;;!!   (interactive "@e")
  896. ;;;!!   (let* ((pos (car event))
  897. ;;;!!      (position (car pos))
  898. ;;;!!      (length (car (cdr pos))))
  899. ;;;!!   (set-window-hscroll (selected-window) 33)))
  900. ;;;!! 
  901. ;;;!! (global-set-key [scroll-bar mouse-1] 'mouse-scroll-up)
  902. ;;;!! (global-set-key [scroll-bar mouse-2] 'mouse-scroll-absolute)
  903. ;;;!! (global-set-key [scroll-bar mouse-3] 'mouse-scroll-down)
  904. ;;;!! 
  905. ;;;!! (global-set-key [vertical-slider mouse-1] 'mouse-scroll-move-cursor)
  906. ;;;!! (global-set-key [vertical-slider mouse-2] 'mouse-scroll-move-cursor)
  907. ;;;!! (global-set-key [vertical-slider mouse-3] 'mouse-scroll-move-cursor)
  908. ;;;!! 
  909. ;;;!! (global-set-key [thumbup mouse-1] 'mouse-scroll-up-full)
  910. ;;;!! (global-set-key [thumbup mouse-2] 'mouse-scroll-up-full)
  911. ;;;!! (global-set-key [thumbup mouse-3] 'mouse-scroll-up-full)
  912. ;;;!! 
  913. ;;;!! (global-set-key [thumbdown mouse-1] 'mouse-scroll-down-full)
  914. ;;;!! (global-set-key [thumbdown mouse-2] 'mouse-scroll-down-full)
  915. ;;;!! (global-set-key [thumbdown mouse-3] 'mouse-scroll-down-full)
  916. ;;;!! 
  917. ;;;!! (global-set-key [horizontal-scroll-bar mouse-1] 'mouse-scroll-left)
  918. ;;;!! (global-set-key [horizontal-scroll-bar mouse-2]
  919. ;;;!!         'mouse-scroll-absolute-horizontally)
  920. ;;;!! (global-set-key [horizontal-scroll-bar mouse-3] 'mouse-scroll-right)
  921. ;;;!! 
  922. ;;;!! (global-set-key [horizontal-slider mouse-1]
  923. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  924. ;;;!! (global-set-key [horizontal-slider mouse-2]
  925. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  926. ;;;!! (global-set-key [horizontal-slider mouse-3]
  927. ;;;!!         'mouse-scroll-move-cursor-horizontally)
  928. ;;;!! 
  929. ;;;!! (global-set-key [thumbleft mouse-1] 'mouse-scroll-left-full)
  930. ;;;!! (global-set-key [thumbleft mouse-2] 'mouse-scroll-left-full)
  931. ;;;!! (global-set-key [thumbleft mouse-3] 'mouse-scroll-left-full)
  932. ;;;!! 
  933. ;;;!! (global-set-key [thumbright mouse-1] 'mouse-scroll-right-full)
  934. ;;;!! (global-set-key [thumbright mouse-2] 'mouse-scroll-right-full)
  935. ;;;!! (global-set-key [thumbright mouse-3] 'mouse-scroll-right-full)
  936. ;;;!! 
  937. ;;;!! (global-set-key [horizontal-scroll-bar S-mouse-2]
  938. ;;;!!         'mouse-split-window-horizontally)
  939. ;;;!! (global-set-key [mode-line S-mouse-2]
  940. ;;;!!         'mouse-split-window-horizontally)
  941. ;;;!! (global-set-key [vertical-scroll-bar S-mouse-2]
  942. ;;;!!         'mouse-split-window)
  943.  
  944. ;;;!! ;;;;
  945. ;;;!! ;;;; Here are experimental things being tested.  Mouse events
  946. ;;;!! ;;;; are of the form:
  947. ;;;!! ;;;;    ((x y) window screen-part key-sequence timestamp)
  948. ;;;!! ;;
  949. ;;;!! ;;;;
  950. ;;;!! ;;;; Dynamically track mouse coordinates
  951. ;;;!! ;;;;
  952. ;;;!! ;;
  953. ;;;!! ;;(defun track-mouse (event)
  954. ;;;!! ;;  "Track the coordinates, absolute and relative, of the mouse."
  955. ;;;!! ;;  (interactive "@e")
  956. ;;;!! ;;  (while mouse-grabbed
  957. ;;;!! ;;    (let* ((pos (read-mouse-position (selected-screen)))
  958. ;;;!! ;;       (abs-x (car pos))
  959. ;;;!! ;;       (abs-y (cdr pos))
  960. ;;;!! ;;       (relative-coordinate (coordinates-in-window-p
  961. ;;;!! ;;                 (list (car pos) (cdr pos))
  962. ;;;!! ;;                 (selected-window))))
  963. ;;;!! ;;      (if (consp relative-coordinate)
  964. ;;;!! ;;      (message "mouse: [%d %d], (%d %d)" abs-x abs-y
  965. ;;;!! ;;           (car relative-coordinate)
  966. ;;;!! ;;           (car (cdr relative-coordinate)))
  967. ;;;!! ;;    (message "mouse: [%d %d]" abs-x abs-y)))))
  968. ;;;!! 
  969. ;;;!! ;;
  970. ;;;!! ;; Dynamically put a box around the line indicated by point
  971. ;;;!! ;;
  972. ;;;!! ;;
  973. ;;;!! ;;(require 'backquote)
  974. ;;;!! ;;
  975. ;;;!! ;;(defun mouse-select-buffer-line (event)
  976. ;;;!! ;;  (interactive "@e")
  977. ;;;!! ;;  (let ((relative-coordinate
  978. ;;;!! ;;     (coordinates-in-window-p (car event) (selected-window)))
  979. ;;;!! ;;    (abs-y (car (cdr (car event)))))
  980. ;;;!! ;;    (if (consp relative-coordinate)
  981. ;;;!! ;;    (progn
  982. ;;;!! ;;      (save-excursion
  983. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  984. ;;;!! ;;        (x-draw-rectangle
  985. ;;;!! ;;         (selected-screen)
  986. ;;;!! ;;         abs-y 0
  987. ;;;!! ;;         (save-excursion
  988. ;;;!! ;;         (move-to-window-line (car (cdr relative-coordinate)))
  989. ;;;!! ;;         (end-of-line)
  990. ;;;!! ;;         (push-mark nil t)
  991. ;;;!! ;;         (beginning-of-line)
  992. ;;;!! ;;         (- (region-end) (region-beginning))) 1))
  993. ;;;!! ;;      (sit-for 1)
  994. ;;;!! ;;      (x-erase-rectangle (selected-screen))))))
  995. ;;;!! ;;
  996. ;;;!! ;;(defvar last-line-drawn nil)
  997. ;;;!! ;;(defvar begin-delim "[^ \t]")
  998. ;;;!! ;;(defvar end-delim   "[^ \t]")
  999. ;;;!! ;;
  1000. ;;;!! ;;(defun mouse-boxing (event)
  1001. ;;;!! ;;  (interactive "@e")
  1002. ;;;!! ;;  (save-excursion
  1003. ;;;!! ;;    (let ((screen (selected-screen)))
  1004. ;;;!! ;;      (while (= (x-mouse-events) 0)
  1005. ;;;!! ;;    (let* ((pos (read-mouse-position screen))
  1006. ;;;!! ;;           (abs-x (car pos))
  1007. ;;;!! ;;           (abs-y (cdr pos))
  1008. ;;;!! ;;           (relative-coordinate
  1009. ;;;!! ;;        (coordinates-in-window-p (` ((, abs-x) (, abs-y)))
  1010. ;;;!! ;;                     (selected-window)))
  1011. ;;;!! ;;           (begin-reg nil)
  1012. ;;;!! ;;           (end-reg nil)
  1013. ;;;!! ;;           (end-column nil)
  1014. ;;;!! ;;           (begin-column nil))
  1015. ;;;!! ;;      (if (and (consp relative-coordinate)
  1016. ;;;!! ;;           (or (not last-line-drawn)
  1017. ;;;!! ;;               (not (= last-line-drawn abs-y))))
  1018. ;;;!! ;;          (progn
  1019. ;;;!! ;;        (move-to-window-line (car (cdr relative-coordinate)))
  1020. ;;;!! ;;        (if (= (following-char) 10)
  1021. ;;;!! ;;            ()
  1022. ;;;!! ;;          (progn
  1023. ;;;!! ;;            (setq begin-reg (1- (re-search-forward end-delim)))
  1024. ;;;!! ;;            (setq begin-column (1- (current-column)))
  1025. ;;;!! ;;            (end-of-line)
  1026. ;;;!! ;;            (setq end-reg (1+ (re-search-backward begin-delim)))
  1027. ;;;!! ;;            (setq end-column (1+ (current-column)))
  1028. ;;;!! ;;            (message "%s" (buffer-substring begin-reg end-reg))
  1029. ;;;!! ;;            (x-draw-rectangle screen
  1030. ;;;!! ;;                      (setq last-line-drawn abs-y)
  1031. ;;;!! ;;                      begin-column
  1032. ;;;!! ;;                      (- end-column begin-column) 1))))))))))
  1033. ;;;!! ;;
  1034. ;;;!! ;;(defun mouse-erase-box ()
  1035. ;;;!! ;;  (interactive)
  1036. ;;;!! ;;  (if last-line-drawn
  1037. ;;;!! ;;      (progn
  1038. ;;;!! ;;    (x-erase-rectangle (selected-screen))
  1039. ;;;!! ;;    (setq last-line-drawn nil))))
  1040. ;;;!! 
  1041. ;;;!! ;;; (defun test-x-rectangle ()
  1042. ;;;!! ;;;   (use-local-mouse-map (setq rectangle-test-map (make-sparse-keymap)))
  1043. ;;;!! ;;;   (define-key rectangle-test-map mouse-motion-button-left 'mouse-boxing)
  1044. ;;;!! ;;;   (define-key rectangle-test-map mouse-button-left-up 'mouse-erase-box))
  1045. ;;;!! 
  1046. ;;;!! ;;
  1047. ;;;!! ;; Here is how to do double clicking in lisp.  About to change.
  1048. ;;;!! ;;
  1049. ;;;!! 
  1050. ;;;!! (defvar double-start nil)
  1051. ;;;!! (defconst double-click-interval 300
  1052. ;;;!!   "Max ticks between clicks")
  1053. ;;;!! 
  1054. ;;;!! (defun double-down (event)
  1055. ;;;!!   (interactive "@e")
  1056. ;;;!!   (if double-start
  1057. ;;;!!       (let ((interval (- (nth 4 event) double-start)))
  1058. ;;;!!     (if (< interval double-click-interval)
  1059. ;;;!!         (progn
  1060. ;;;!!           (backward-up-list 1)
  1061. ;;;!!           ;;      (message "Interval %d" interval)
  1062. ;;;!!           (sleep-for 1)))
  1063. ;;;!!     (setq double-start nil))
  1064. ;;;!!     (setq double-start (nth 4 event))))
  1065. ;;;!!     
  1066. ;;;!! (defun double-up (event)
  1067. ;;;!!   (interactive "@e")
  1068. ;;;!!   (and double-start
  1069. ;;;!!        (> (- (nth 4 event ) double-start) double-click-interval)
  1070. ;;;!!        (setq double-start nil)))
  1071. ;;;!! 
  1072. ;;;!! ;;; (defun x-test-doubleclick ()
  1073. ;;;!! ;;;   (use-local-mouse-map (setq doubleclick-test-map (make-sparse-keymap)))
  1074. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left 'double-down)
  1075. ;;;!! ;;;   (define-key doubleclick-test-map mouse-button-left-up 'double-up))
  1076. ;;;!! 
  1077. ;;;!! ;;
  1078. ;;;!! ;; This scrolls while button is depressed.  Use preferable in scroll bar.
  1079. ;;;!! ;;
  1080. ;;;!! 
  1081. ;;;!! (defvar scrolled-lines 0)
  1082. ;;;!! (defconst scroll-speed 1)
  1083. ;;;!! 
  1084. ;;;!! (defun incr-scroll-down (event)
  1085. ;;;!!   (interactive "@e")
  1086. ;;;!!   (setq scrolled-lines 0)
  1087. ;;;!!   (incremental-scroll scroll-speed))
  1088. ;;;!! 
  1089. ;;;!! (defun incr-scroll-up (event)
  1090. ;;;!!   (interactive "@e")
  1091. ;;;!!   (setq scrolled-lines 0)
  1092. ;;;!!   (incremental-scroll (- scroll-speed)))
  1093. ;;;!! 
  1094. ;;;!! (defun incremental-scroll (n)
  1095. ;;;!!   (while (= (x-mouse-events) 0)
  1096. ;;;!!     (setq scrolled-lines (1+ (* scroll-speed scrolled-lines)))
  1097. ;;;!!     (scroll-down n)
  1098. ;;;!!     (sit-for 300 t)))
  1099. ;;;!! 
  1100. ;;;!! (defun incr-scroll-stop (event)
  1101. ;;;!!   (interactive "@e")
  1102. ;;;!!   (message "Scrolled %d lines" scrolled-lines)
  1103. ;;;!!   (setq scrolled-lines 0)
  1104. ;;;!!   (sleep-for 1))
  1105. ;;;!! 
  1106. ;;;!! ;;; (defun x-testing-scroll ()
  1107. ;;;!! ;;;   (let ((scrolling-map (function mouse-vertical-scroll-bar-prefix)))
  1108. ;;;!! ;;;     (define-key scrolling-map mouse-button-left 'incr-scroll-down)
  1109. ;;;!! ;;;     (define-key scrolling-map mouse-button-right 'incr-scroll-up)
  1110. ;;;!! ;;;     (define-key scrolling-map mouse-button-left-up 'incr-scroll-stop)
  1111. ;;;!! ;;;     (define-key scrolling-map mouse-button-right-up 'incr-scroll-stop)))
  1112. ;;;!! 
  1113. ;;;!! ;;
  1114. ;;;!! ;; Some playthings suitable for picture mode?  They need work.
  1115. ;;;!! ;;
  1116. ;;;!! 
  1117. ;;;!! (defun mouse-kill-rectangle (event)
  1118. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1119. ;;;!!   (interactive "@e")
  1120. ;;;!!   (let ((point-save (point)))
  1121. ;;;!!     (save-excursion
  1122. ;;;!!       (mouse-set-point event)
  1123. ;;;!!       (push-mark nil t)
  1124. ;;;!!       (if (> point-save (point))
  1125. ;;;!!       (kill-rectangle (point) point-save)
  1126. ;;;!!     (kill-rectangle point-save (point))))))
  1127. ;;;!! 
  1128. ;;;!! (defun mouse-open-rectangle (event)
  1129. ;;;!!   "Kill the rectangle between point and the mouse cursor."
  1130. ;;;!!   (interactive "@e")
  1131. ;;;!!   (let ((point-save (point)))
  1132. ;;;!!     (save-excursion
  1133. ;;;!!       (mouse-set-point event)
  1134. ;;;!!       (push-mark nil t)
  1135. ;;;!!       (if (> point-save (point))
  1136. ;;;!!       (open-rectangle (point) point-save)
  1137. ;;;!!     (open-rectangle point-save (point))))))
  1138. ;;;!! 
  1139. ;;;!! ;; Must be a better way to do this.
  1140. ;;;!! 
  1141. ;;;!! (defun mouse-multiple-insert (n char)
  1142. ;;;!!   (while (> n 0)
  1143. ;;;!!     (insert char)
  1144. ;;;!!     (setq n (1- n))))
  1145. ;;;!! 
  1146. ;;;!! ;; What this could do is not finalize until button was released.
  1147. ;;;!! 
  1148. ;;;!! (defun mouse-move-text (event)
  1149. ;;;!!   "Move text from point to cursor position, inserting spaces."
  1150. ;;;!!   (interactive "@e")
  1151. ;;;!!   (let* ((relative-coordinate
  1152. ;;;!!       (coordinates-in-window-p (car event) (selected-window))))
  1153. ;;;!!     (if (consp relative-coordinate)
  1154. ;;;!!     (cond ((> (current-column) (car relative-coordinate))
  1155. ;;;!!            (delete-char
  1156. ;;;!!         (- (car relative-coordinate) (current-column))))
  1157. ;;;!!           ((< (current-column) (car relative-coordinate))
  1158. ;;;!!            (mouse-multiple-insert
  1159. ;;;!!         (- (car relative-coordinate) (current-column)) " "))
  1160. ;;;!!           ((= (current-column) (car relative-coordinate)) (ding))))))
  1161.  
  1162. ;; Choose a completion with the mouse.
  1163.  
  1164. ;; Delete the longest partial match for STRING
  1165. ;; that can be found before POINT.
  1166. (defun mouse-delete-max-match (string)
  1167.   (let ((len (min (length string)
  1168.           (- (point-max) (point-min)))))
  1169.     (goto-char (max (point-min) (- (point) (length string))))
  1170.     (while (and (> len 0)
  1171.         (let ((tail (buffer-substring (point)
  1172.                           (+ (point) len))))
  1173.           (not (string= tail (substring string 0 len)))))
  1174.       (setq len (1- len))
  1175.       (forward-char 1))
  1176.     (delete-char len)))
  1177.  
  1178. (defun mouse-choose-completion (event)
  1179.   "Click on an alternative in the `*Completions*' buffer to choose it."
  1180.   (interactive "e")
  1181.   (let ((buffer (window-buffer))
  1182.         choice)
  1183.     (save-excursion
  1184.       (set-buffer (window-buffer (posn-window (event-start event))))
  1185.       (save-excursion
  1186.     (goto-char (posn-point (event-start event)))
  1187.     (skip-chars-backward "^ \t\n")
  1188.     (let ((beg (point)))
  1189.       (skip-chars-forward "^ \t\n")
  1190.       (setq choice (buffer-substring beg (point))))))
  1191.     (set-buffer buffer)
  1192.     (mouse-delete-max-match choice)
  1193.     (insert choice)
  1194.     (and (equal buffer (window-buffer (minibuffer-window)))
  1195.      (minibuffer-complete-and-exit))))
  1196.  
  1197. ;; Font selection.
  1198.  
  1199. (defun font-menu-add-default ()
  1200.   (let* ((default (cdr (assq 'font (frame-parameters (selected-frame)))))
  1201.      (font-alist x-fixed-font-alist)
  1202.      (elt (or (assoc "Misc" font-alist) (nth 1 font-alist))))
  1203.     (if (assoc "Default" elt)
  1204.     (delete (assoc "Default" elt) elt))
  1205.     (setcdr elt
  1206.         (cons (list "Default"
  1207.             (cdr (assq 'font (frame-parameters (selected-frame)))))
  1208.           (cdr elt)))))
  1209.  
  1210. (defvar x-fixed-font-alist
  1211.   '("Font menu"
  1212.     ("Misc"
  1213.      ("6x10" "-misc-fixed-medium-r-semicondensed--10-110-75-75-c-60-*-1")
  1214.      ("6x12" "-misc-fixed-medium-r-semicondensed--12-110-75-75-c-60-*-1")
  1215.      ("6x13" "-misc-fixed-medium-r-semicondensed--13-120-75-75-c-60-*-1")
  1216.      ("lucida 13"
  1217.       "-b&h-lucidatypewriter-medium-r-normal-sans-0-0-0-0-m-0-*-1")
  1218.      ("7x13" "-misc-fixed-medium-r-normal--13-120-75-75-c-70-*-1")
  1219.      ("7x14" "-misc-fixed-medium-r-normal--14-130-75-75-c-70-*-1")
  1220.      ("9x15" "-misc-fixed-medium-r-normal--15-140-*-*-c-*-*-1")
  1221.      ("")
  1222.      ("clean 8x8" "-schumacher-clean-medium-r-normal--*-80-*-*-c-*-*-1")
  1223.      ("clean 8x14" "-schumacher-clean-medium-r-normal--*-140-*-*-c-*-*-1")
  1224.      ("clean 8x10" "-schumacher-clean-medium-r-normal--*-100-*-*-c-*-*-1")
  1225.      ("clean 8x16" "-schumacher-clean-medium-r-normal--*-160-*-*-c-*-*-1")
  1226.      ("")
  1227.      ("sony 8x16" "-sony-fixed-medium-r-normal--16-120-100-100-c-80-*-1")
  1228.      ("")
  1229.      ("fixed" "fixed")
  1230.      ("10x20" "10x20")
  1231.      ("11x18" "11x18")
  1232.      ("12x24" "12x24"))
  1233. ;;; We don't seem to have these; who knows what they are.
  1234. ;;;    ("fg-18" "fg-18")
  1235. ;;;    ("fg-25" "fg-25")
  1236. ;;;    ("lucidasanstypewriter-12" "lucidasanstypewriter-12")
  1237. ;;;    ("lucidasanstypewriter-bold-14" "lucidasanstypewriter-bold-14")
  1238. ;;;    ("lucidasanstypewriter-bold-24" "lucidasanstypewriter-bold-24")
  1239. ;;;    ("lucidatypewriter-bold-r-24" "-b&h-lucidatypewriter-bold-r-normal-sans-24-240-75-75-m-140-iso8859-1")
  1240. ;;;    ("fixed-medium-20" "-misc-fixed-medium-*-*-*-20-*-*-*-*-*-*-*")
  1241.     ("Courier"
  1242.      ("8" "-adobe-courier-medium-r-normal--*-80-*-*-m-*-iso8859-1")
  1243.      ("10" "-adobe-courier-medium-r-normal--*-100-*-*-m-*-iso8859-1")
  1244.      ("12" "-adobe-courier-medium-r-normal--*-120-*-*-m-*-iso8859-1")
  1245.      ("14" "-adobe-courier-medium-r-normal--*-140-*-*-m-*-iso8859-1")
  1246.      ("18" "-adobe-courier-medium-r-normal--*-180-*-*-m-*-iso8859-1")
  1247.      ("24" "-adobe-courier-medium-r-normal--*-240-*-*-m-*-iso8859-1")
  1248.      ("8 bold" "-adobe-courier-bold-r-normal--*-80-*-*-m-*-iso8859-1")
  1249.      ("10 bold" "-adobe-courier-bold-r-normal--*-100-*-*-m-*-iso8859-1")
  1250.      ("12 bold" "-adobe-courier-bold-r-normal--*-120-*-*-m-*-iso8859-1")
  1251.      ("14 bold" "-adobe-courier-bold-r-normal--*-140-*-*-m-*-iso8859-1")
  1252.      ("18 bold" "-adobe-courier-bold-r-normal--*-180-*-*-m-*-iso8859-1")
  1253.      ("24 bold" "-adobe-courier-bold-r-normal--*-240-*-*-m-*-iso8859-1")
  1254.      ("8 slant" "-adobe-courier-medium-o-normal--*-80-*-*-m-*-iso8859-1")
  1255.      ("10 slant" "-adobe-courier-medium-o-normal--*-100-*-*-m-*-iso8859-1")
  1256.      ("12 slant" "-adobe-courier-medium-o-normal--*-120-*-*-m-*-iso8859-1")
  1257.      ("14 slant" "-adobe-courier-medium-o-normal--*-140-*-*-m-*-iso8859-1")
  1258.      ("18 slant" "-adobe-courier-medium-o-normal--*-180-*-*-m-*-iso8859-1")
  1259.      ("24 slant" "-adobe-courier-medium-o-normal--*-240-*-*-m-*-iso8859-1")
  1260.      ("8 bold slant" "-adobe-courier-bold-o-normal--*-80-*-*-m-*-iso8859-1")
  1261.      ("10 bold slant" "-adobe-courier-bold-o-normal--*-100-*-*-m-*-iso8859-1")
  1262.      ("12 bold slant" "-adobe-courier-bold-o-normal--*-120-*-*-m-*-iso8859-1")
  1263.      ("14 bold slant" "-adobe-courier-bold-o-normal--*-140-*-*-m-*-iso8859-1")
  1264.      ("18 bold slant" "-adobe-courier-bold-o-normal--*-180-*-*-m-*-iso8859-1")
  1265.      ("24 bold slant" "-adobe-courier-bold-o-normal--*-240-*-*-m-*-iso8859-1"))
  1266.     )
  1267.   "X fonts suitable for use in Emacs.")
  1268.  
  1269. (defun mouse-set-font (&optional font)
  1270.   "Select an emacs font from a list of known good fonts"
  1271.   (interactive
  1272.    (x-popup-menu last-nonmenu-event x-fixed-font-alist))
  1273.   (if font
  1274.       (progn (modify-frame-parameters (selected-frame)
  1275.                       (list (cons 'font font)))
  1276.          ;; Update some standard faces too.
  1277.          (set-face-font 'bold nil (selected-frame)) 
  1278.          (make-face-bold 'bold (selected-frame) t)
  1279.          (set-face-font 'italic nil (selected-frame))
  1280.          (make-face-italic 'italic (selected-frame) t)
  1281.          (set-face-font 'bold-italic nil (selected-frame))
  1282.          (make-face-bold-italic 'bold-italic (selected-frame) t)
  1283.          ;; Update any nonstandard faces whose definition is
  1284.          ;; "a bold/italic/bold&italic version of the frame's font".
  1285.          (let ((rest global-face-data))
  1286.            (while rest
  1287.          (condition-case nil
  1288.              (if (listp (face-font (cdr (car rest))))
  1289.              (let ((bold (memq 'bold (face-font (cdr (car rest)))))
  1290.                    (italic (memq 'italic (face-font (cdr (car rest))))))
  1291.                (if (and bold italic)
  1292.                    (make-face-bold-italic (car (car rest)) (selected-frame))
  1293.                  (if bold
  1294.                  (make-face-bold (car (car rest)) (selected-frame))
  1295.                    (if italic
  1296.                    (make-face-italic (car (car rest)) (selected-frame)))))))
  1297.            (error nil))
  1298.          (setq rest (cdr rest))))
  1299.          )))
  1300.  
  1301. ;;; Bindings for mouse commands.
  1302.  
  1303. (define-key global-map [down-mouse-1] 'mouse-drag-region)
  1304. (global-set-key [mouse-1]    'mouse-set-point)
  1305. (global-set-key [drag-mouse-1]    'mouse-set-region)
  1306.  
  1307. ;; These are tested for in mouse-drag-region.
  1308. (global-set-key [double-mouse-1] 'mouse-set-point)
  1309. (global-set-key [triple-mouse-1] 'mouse-set-point)
  1310.  
  1311. (global-set-key [mouse-2]    'mouse-yank-at-click)
  1312. (global-set-key [mouse-3]    'mouse-save-then-kill)
  1313.  
  1314. ;; By binding these to down-going events, we let the user use the up-going
  1315. ;; event to make the selection, saving a click.
  1316. (global-set-key [C-down-mouse-1]    'mouse-buffer-menu)
  1317. (global-set-key [C-down-mouse-3]    'mouse-set-font)
  1318.  
  1319. ;; Replaced with dragging mouse-1
  1320. ;; (global-set-key [S-mouse-1]    'mouse-set-mark)
  1321.  
  1322. (global-set-key [mode-line mouse-1] 'mouse-delete-other-windows)
  1323. (global-set-key [mode-line mouse-3] 'mouse-delete-window)
  1324. (global-set-key [mode-line S-mouse-2] 'mouse-split-window-horizontally)
  1325.  
  1326. ;; Define the mouse help menu tree.
  1327.  
  1328. (defvar help-menu-map '(keymap "Help"))
  1329. (global-set-key [C-down-mouse-2] help-menu-map)
  1330.  
  1331. (defvar help-apropos-map (make-sparse-keymap "Is there a command that..."))
  1332. (defvar help-keys-map (make-sparse-keymap "Key Commands <==> Functions"))
  1333. (defvar help-manual-map (make-sparse-keymap "Manual and tutorial"))
  1334. (defvar help-misc-map (make-sparse-keymap "Odds and ends"))
  1335. (defvar help-modes-map (make-sparse-keymap "Modes"))
  1336. (defvar help-admin-map (make-sparse-keymap "Administrivia"))
  1337.  
  1338. (define-key help-menu-map [apropos]
  1339.   (cons "@Is there a command that..." help-apropos-map))
  1340. (define-key help-menu-map [keys]
  1341.   (cons "@Key Commands <==> Functions" help-keys-map))
  1342. (define-key help-menu-map [manuals]
  1343.   (cons "@Manual and tutorial" help-manual-map))
  1344. (define-key help-menu-map [misc]
  1345.   (cons "@Odds and ends" help-misc-map))
  1346. (define-key help-menu-map [modes]
  1347.   (cons "@Modes" help-modes-map))
  1348. (define-key help-menu-map [admin]
  1349.   (cons "@Administrivia" help-admin-map))
  1350.  
  1351. (define-key help-apropos-map "c" '("Command Apropos" . command-apropos))
  1352. (define-key help-apropos-map "a" '("Apropos" . apropos))
  1353.  
  1354. (define-key help-keys-map "b"
  1355.   '("List all keystroke commands" . describe-bindings))
  1356. (define-key help-keys-map "c"
  1357.   '("Describe key briefly" . describe-key-briefly))
  1358. (define-key help-keys-map "k"
  1359.   '("Describe key verbose" . describe-key))
  1360. (define-key help-keys-map "f"
  1361.   '("Describe Lisp function" . describe-function))
  1362. (define-key help-keys-map "w"
  1363.   '("Where is this command" . where-is))
  1364.  
  1365. (define-key help-manual-map "i" '("Info system" . info))
  1366. (define-key help-manual-map "t"
  1367.   '("Invoke Emacs tutorial" . help-with-tutorial))
  1368.  
  1369. (define-key help-misc-map "l" '("Last 100 Keystrokes" . view-lossage))
  1370. (define-key help-misc-map "s" '("Describe syntax table" . describe-syntax))
  1371.  
  1372. (define-key help-modes-map "m"
  1373.   '("Describe current major mode" . describe-mode))
  1374. (define-key help-modes-map "b"
  1375.   '("List all keystroke commands" . describe-bindings))
  1376.  
  1377. (define-key help-admin-map "n"
  1378.   '("View Emacs news" . view-emacs-news))
  1379. (define-key help-admin-map "l"
  1380.   '("View Emacs copying conditions" . describe-copying))
  1381. (define-key help-admin-map "d"
  1382.   '("Describe distribution" . describe-distribution))
  1383. (define-key help-admin-map "w"
  1384.   '("Describe (non)warranty" . describe-no-warranty))
  1385.  
  1386. (provide 'mouse)
  1387.  
  1388. ;;; mouse.el ends here
  1389.